<?php
/**
 * File to handle all API requests
 * Accepts GET and POST
 *
 * Each request will be identified by TAG
 * Response will be JSON data
 * check for POST request
 */
if (isset($_POST['tag']) && $_POST['tag'] != '') 
{
    // get tag
    $tag = $_POST['tag'];
 
    // include db handler
    require_once 'DB_Functions.php';
    $db = new DB_Functions();
 
    // response Array
    $response = array("tag" => $tag, "success" => 0, "error" => 0);
 
    // check for tag type
    if ($tag == 'login') 
    {
        // Request type is check Login
        $email = $_POST['email'];
        $password = $_POST['password'];
 
        // check for user
        $user = $db->getUserByEmailAndPassword($email, $password);
    if ($user != false) 
    {
            // user found
            // echo json with success = 1
            $response["success"] = 1;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
    } 
    else 
    {
            // user not found
            // echo json with error = 1
            $response["error"] = 1;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }
    } 
    else if ($tag == 'register') 
    {
        // Request type is Register new user
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = $_POST['password'];
 
        // check if user is already existed
    if ($db->isUserExisted($email)) 
    {
            // user is already existed - error response
            $response["error"] = 2;
            $response["error_msg"] = "User already existed";
            echo json_encode($response);
    } 
    else 
    {
            // store user
            $user = $db->storeUser($name, $email, $password);
        if ($user) 
        {
                // user stored successfully
                $response["success"] = 1;
                $response["uid"] = $user["unique_id"];
                $response["user"]["name"] = $user["name"];
                $response["user"]["email"] = $user["email"];
                $response["user"]["created_at"] = $user["created_at"];
                $response["user"]["updated_at"] = $user["updated_at"];
                echo json_encode($response);
        } 
        else 
        {
                // user failed to store
                $response["error"] = 1;
                $response["error_msg"] = "Error occured in Registartion";
                echo json_encode($response);
            }
        }
    } 
    else if ($tag == 'searchItems') 
    {
       $item_name = $_POST['item_name'];
        if(isset($_POST['place_uuid']))
        {
            $place_id=$_POST['place_uuid'];
            $items = $db->searchItemsByPlace($item_name,$place_id);
        }
        else
        {
            $items = $db->searchItems($item_name);
        }
        echo json_encode($items);
    } 
    else if ($tag == 'searchPlaces')
    {
        $place_name = $_POST['place_name'];
        $places = $db->searchPlaces($place_name);
        echo json_encode($places);
    }
    else if ($tag == 'getItemById')
    {
        $item_id = $_POST['item_uuid'];
        $item = $db->getItemById($item_id);
        echo json_encode($item);
    }
    else if ($tag == 'getPlaceById')
    {
        $place_id = $_POST['place_uuid'];
        $place = $db->getPlaceById($place_id);
        echo json_encode($place);
    }
    else if ($tag == 'getPlaceMaps')
    {
        $place_id = $_POST['place_uuid'];
        $placemaps = $db->getPlaceMaps($place_id);
        echo json_encode($placemaps);
    }
    else if ($tag == 'getPlaceMapByID')
    {
        $place_id = $_POST['placemap_uuid'];
        $placemap = $db->getPlaceMapByID($place_id);
        echo json_encode($placemap);
    }
    else if ($tag == 'getItemList')
    {
        $user_id = $_POST['user_uuid'];
        $place_id = $_POST['place_uuid'];
        $items = $db->getItemList($user_id,$place_id);
        echo json_encode($items);
    }
    else if ($tag == 'addtoItemList')
    {
        $user_id = $_POST['user_uuid'];
        $item_id = $_POST['item_uuid'];
        $added = $db->addtoItemList($user_id,$item_id);
        if($added){
            $result[]=true;
        }else{
            $result[]=false;
        }
        echo json_encode($result);
    }
    else if ($tag == 'getPlaceList')
    {
        $user_id = $_POST['user_uuid'];
        $places = $db->getPlaceList($user_id);
        echo json_encode($places);
    }
    else if ($tag == 'radiusSearch')
    {
        $radius = $_POST['radius'];
        $lat = $_POST['latitude'];
        $lon = $_POST['longitude'];
        $places = $db->radiusSearch($lat,$lon,$radius);
        echo json_encode($places);
    }
    else if ($tag == 'addtoPlaceList')
    {
        $user_id = $_POST['user_uuid'];
        $place_id = $_POST['place_uuid'];
        $added = $db->addtoPlaceList($user_id,$place_id);
        if($added){
            $result[]=true;
        }else{
            $result[]=false;
        }
        echo json_encode($result);
    }
    else if ($tag == 'addItemLocation')
    {
        $item_id = $_POST['item_uuid'];
        $loc_x = $_POST['loc_x'];
        $loc_y = $_POST['loc_y'];
        $loc_z = $_POST['loc_z'];
        $weight = $_POST['weight'];
        $added = $db->addItemLocation($item_id,$loc_x,$loc_y,$loc_z,$weight);
        if($added){
            $result[]=true;
        }else{
            $result[]=false;
        }
        echo json_encode($result);
    }
    else if ($tag == 'addItem')
    {
        $item_name = $_POST['item_name'];
        $item_descrip = $_POST['item_descrip'];
        $place_id = $_POST['place_uuid'];
        $loc_x = $_POST['loc_x'];
        $loc_y = $_POST['loc_y'];
        $loc_z = $_POST['loc_z'];
        $user_id = $_POST['user_uuid'];
        if(isset($_POST['timeout']))
        {
            $timeo=$_POST['timeout'];
            $result = $db->addItemWithTimeout($item_name,$item_descrip,$place_id,$loc_x,$loc_y,$loc_z,$user_id,$timeo);
        }
        else
        {
            $result = $db->addItem($item_name,$item_descrip,$place_id,$loc_x,$loc_y,$loc_z,$user_id);
        }
        $rarray=array("success"=>0);
        if($result){
            $rarray["item_id"]=$result;
            $rarray["success"]=1;
        }
        $array[]=$rarray;
        echo json_encode($array);
    }
    
    else 
    {
        echo "Invalid Request";
    }
} 
else 
{
      echo "Access Denied";
}
?>
